package com.cloudptt.api.util;

import java.io.UnsupportedEncodingException;

public class GPSUtil {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		// try {
		// String i=null;
		//
		// i.length();
		// } catch (Exception e) {
		// System.out.println(e.getMessage());
		// }

		// unicode2String("007300680069007A00680065006E00");

		// string2Unicode("���01");
		// chinaToUnicode("a");
		// GPS2String(22.23211,103.123121);
		string2ChineseStr("6231613362366138633463666366633264306430633164306233623533326264643362646663626466383333623563306364613362336235");

		// toStringHex1("b1a3b6a8c4cfcfc2d0d0c1d0b3b532bdd3bdfcbdf833b5c0cda3b3b5");

	}

	public static String unicode2String(String unicodeStr) {
		StringBuffer sb = new StringBuffer();
		int k = 0;
		String tmp = null;
		while (unicodeStr.length() >= 4) {

			tmp = new String(unicodeStr.substring(0, 4).toString());
			unicodeStr = unicodeStr.substring(4, unicodeStr.length());
			char c = (char) Integer.parseInt(tmp, 16);
			sb.append(c);
			k++;
		}
		return sb.toString();
	}

	public static String string2Unicode(String s) {
		try {
			StringBuffer out = new StringBuffer("");
			StringBuffer outtmp = new StringBuffer("");
			byte[] bytes = s.getBytes("UnicodeBig");
			for (int i = 2; i < bytes.length - 1; i += 2) {
				String str = Integer.toHexString(bytes[i + 1] & 0xff);
				for (int j = str.length(); j < 2; j++) {
					out.append("0");
				}
				String str1 = Integer.toHexString(bytes[i] & 0xff);
				str1 = String.format("%02x", bytes[i]);

				out.append(str);
				out.append(str1);
			}
//			System.out.println(out.toString().toLowerCase());

			char[] c1 = out.toString().toLowerCase().toCharArray();
			for (int i = 0; i < c1.length; i++) {
				outtmp.append(Integer.toHexString(c1[i] & 0xff));
			}
			outtmp.append("30303030");// ���ֽ����
//			System.out.println(outtmp.toString().toLowerCase());
			return outtmp.toString().toLowerCase();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String GPS2String(double lat, double lot) {

		System.out.println(lot);
		StringBuffer out = new StringBuffer("");
		String latStr = String.valueOf(lat);
		String lotStr = String.valueOf(lot);
		String tmp = latStr + "," + lotStr;
//		System.out.println(tmp.toString().toLowerCase());
		char[] c1 = tmp.toString().toLowerCase().toCharArray();
		for (int i = 0; i < c1.length; i++) {
			out.append(Integer.toHexString(c1[i] & 0xff));
		}
		out.append("30303030");// 
//		System.out.println(out.toString().toLowerCase());
		return out.toString().toLowerCase();

	}

	/**
	 * ��Asciiת���������ַ�
	 */
	public static String string2ChineseStr(String s) {
		String reStr = null;
	
		byte[] baKeyword = new byte[s.length() / 2];
		for (int i = 0; i < baKeyword.length; i++) {
			try {
				baKeyword[i] = (byte) (0xff & Integer.parseInt(
						s.substring(i * 2, i * 2 + 2), 16));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		try {
			s = new String(baKeyword, "ASCII");
			
			byte[] bAscii = new byte[s.length() / 2];
			for (int i = 0; i < bAscii.length; i++) {
				try {
					bAscii[i] = (byte) (0xff & Integer.parseInt(
							s.substring(i * 2, i * 2 + 2), 16));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			reStr= new String(bAscii, "gbk").toString();
			System.out.println(reStr);
		} catch (Exception e1) {
			e1.printStackTrace();
		}

//		try {
//			String strtm = new String(s.getBytes("ASCII"), "gbk");
//
//			
//			System.out.println(" " + new String(bAscii, "gbk").toString());
//			System.out.println(strtm);
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}

		return reStr;
	}
	
	

	static double DEF_PI = 3.14159265359; // PI
	static double DEF_2PI = 6.28318530712; // 2*PI
	static double DEF_PI180 = 0.01745329252; // PI/180.0
	static double DEF_R = 6370693.5; // radius of earth
	
	/**
	 * 第一种方式，按勾股定律求结果
	 * @param lon1
	 * @param lat1
	 * @param lon2
	 * @param lat2
	 * @return
	 */
	public static double GetShortDistance(double lon1, double lat1, double lon2,
			double lat2) {
		double ew1, ns1, ew2, ns2;
		double dx, dy, dew;
		double distance;
		// 角度转换为弧度
		ew1 = lon1 * DEF_PI180;
		ns1 = lat1 * DEF_PI180;
		ew2 = lon2 * DEF_PI180;
		ns2 = lat2 * DEF_PI180;
		// 经度差
		dew = ew1 - ew2;
		// 若跨东经和西经180 度，进行调整
		if (dew > DEF_PI)
			dew = DEF_2PI - dew;
		else if (dew < -DEF_PI)
			dew = DEF_2PI + dew;
		dx = DEF_R * Math.cos(ns1) * dew; // 东西方向长度(在纬度圈上的投影长度)
		dy = DEF_R * (ns1 - ns2); // 南北方向长度(在经度圈上的投影长度)
		// 勾股定理求斜边长
		distance = Math.sqrt(dx * dx + dy * dy);
		return distance;
	}

	/**
	 * 第二种方式，按大圆劣弧求距离
	 * @param lon1
	 * @param lat1
	 * @param lon2
	 * @param lat2
	 * @return
	 */
	public static double GetLongDistance(double lon1, double lat1, double lon2,
			double lat2) {
		double ew1, ns1, ew2, ns2;
		double distance;
		// 角度转换为弧度
		ew1 = lon1 * DEF_PI180;
		ns1 = lat1 * DEF_PI180;
		ew2 = lon2 * DEF_PI180;
		ns2 = lat2 * DEF_PI180;
		// 求大圆劣弧与球心所夹的角(弧度)
		distance = Math.sin(ns1) * Math.sin(ns2) + Math.cos(ns1)
				* Math.cos(ns2) * Math.cos(ew1 - ew2);
		// 调整到[-1..1]范围内，避免溢出
		if (distance > 1.0)
			distance = 1.0;
		else if (distance < -1.0)
			distance = -1.0;
		// 求大圆劣弧长度
		distance = DEF_R * Math.acos(distance);
		return distance;
	}
}
